<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>跨平台文件选择器服务 - 演示页面</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
            display: flex;
            align-items: flex-start;
            justify-content: center;
            padding-top: 5vh;
        }

        .container {
            max-width: 1200px;
            width: 100%;
            background: white;
            border-radius: 20px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            overflow: hidden;
            animation: slideInFromTop 0.8s ease-out;
        }

        @keyframes slideInFromTop {
            0% {
                transform: translateY(-100px);
                opacity: 0;
            }
            100% {
                transform: translateY(0);
                opacity: 1;
            }
        }

        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 30px;
            text-align: center;
            animation: fadeInDown 1s ease-out 0.3s both;
        }

        @keyframes fadeInDown {
            0% {
                opacity: 0;
                transform: translateY(-20px);
            }
            100% {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .header h1 {
            font-size: 2.5em;
            margin-bottom: 10px;
            font-weight: 300;
            animation: bounce 2s ease-out 1s infinite;
        }

        @keyframes bounce {
            0%, 20%, 50%, 80%, 100% {
                transform: translateY(0);
            }
            40% {
                transform: translateY(-10px);
            }
            60% {
                transform: translateY(-5px);
            }
        }

        .header p {
            font-size: 1.1em;
            opacity: 0.9;
        }

        .content {
            padding: 40px;
            animation: fadeInUp 1s ease-out 0.5s both;
        }

        @keyframes fadeInUp {
            0% {
                opacity: 0;
                transform: translateY(30px);
            }
            100% {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .status-bar {
            background: #f8f9fa;
            padding: 15px 20px;
            border-radius: 10px;
            margin-bottom: 30px;
            border-left: 4px solid #28a745;
            animation: slideInFromLeft 0.6s ease-out 0.7s both;
        }

        @keyframes slideInFromLeft {
            0% {
                transform: translateX(-50px);
                opacity: 0;
            }
            100% {
                transform: translateX(0);
                opacity: 1;
            }
        }

        .status-indicator {
            display: inline-block;
            width: 10px;
            height: 10px;
            border-radius: 50%;
            margin-right: 10px;
            animation: heartbeat 1.5s ease-in-out infinite;
        }

        .status-online {
            background: #28a745;
        }

        .status-offline {
            background: #dc3545;
        }

        @keyframes heartbeat {
            0% {
                transform: scale(1);
                box-shadow: 0 0 0 0 rgba(40, 167, 69, 0.7);
            }
            70% {
                transform: scale(1.05);
                box-shadow: 0 0 0 10px rgba(40, 167, 69, 0);
            }
            100% {
                transform: scale(1);
                box-shadow: 0 0 0 0 rgba(40, 167, 69, 0);
            }
        }

        .demo-section {
            margin-bottom: 40px;
            padding: 30px;
            border: 1px solid #e9ecef;
            border-radius: 15px;
            background: #fafafa;
            animation: scaleIn 0.5s ease-out;
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }

        .demo-section:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
        }

        @keyframes scaleIn {
            0% {
                transform: scale(0.9);
                opacity: 0;
            }
            100% {
                transform: scale(1);
                opacity: 1;
            }
        }

        .demo-section h2 {
            color: #495057;
            margin-bottom: 20px;
            font-size: 1.5em;
            font-weight: 500;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            color: #495057;
        }

        .form-control {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid #e9ecef;
            border-radius: 8px;
            font-size: 16px;
            transition: border-color 0.3s ease;
        }

        .form-control:focus {
            outline: none;
            border-color: #667eea;
            animation: inputGlow 0.3s ease-out;
        }

        @keyframes inputGlow {
            0% {
                box-shadow: 0 0 0 0 rgba(102, 126, 234, 0.5);
            }
            100% {
                box-shadow: 0 0 0 5px rgba(102, 126, 234, 0.1);
            }
        }

        .checkbox-group {
            display: flex;
            align-items: center;
            margin-bottom: 15px;
        }

        .checkbox-group input[type="checkbox"] {
            margin-right: 10px;
            transform: scale(1.2);
        }

        .btn {
            padding: 12px 30px;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-right: 10px;
            margin-bottom: 10px;
        }

        .btn-primary {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
        }

        .btn-primary:hover {
            transform: translateY(-3px) scale(1.05);
            box-shadow: 0 8px 20px rgba(102, 126, 234, 0.4);
            animation: wiggle 0.5s ease-in-out;
        }

        @keyframes wiggle {
            0%, 7% { transform: translateY(-3px) scale(1.05) rotateZ(0); }
            15% { transform: translateY(-3px) scale(1.05) rotateZ(-5deg); }
            20% { transform: translateY(-3px) scale(1.05) rotateZ(5deg); }
            25% { transform: translateY(-3px) scale(1.05) rotateZ(-5deg); }
            30% { transform: translateY(-3px) scale(1.05) rotateZ(5deg); }
            35% { transform: translateY(-3px) scale(1.05) rotateZ(-5deg); }
            40%, 100% { transform: translateY(-3px) scale(1.05) rotateZ(0); }
        }

        .btn-secondary {
            background: #6c757d;
            color: white;
        }

        .btn-secondary:hover {
            background: #5a6268;
            transform: translateY(-2px) rotateY(5deg);
        }

        .btn-success {
            background: #28a745;
            color: white;
        }

        .btn-success:hover {
            background: #218838;
            transform: translateY(-2px) rotateX(5deg);
        }

        .results {
            margin-top: 30px;
            padding: 20px;
            background: white;
            border-radius: 10px;
            border: 1px solid #e9ecef;
            max-height: 500px;
            overflow-y: auto;
            animation: slideInFromBottom 0.6s ease-out;
        }

        @keyframes slideInFromBottom {
            0% {
                transform: translateY(50px);
                opacity: 0;
            }
            100% {
                transform: translateY(0);
                opacity: 1;
            }
        }

        .results h3 {
            color: #495057;
            margin-bottom: 15px;
            font-size: 1.3em;
        }

        .file-item {
            background: #f8f9fa;
            padding: 15px;
            margin-bottom: 10px;
            border-radius: 8px;
            border-left: 4px solid #667eea;
            animation: slideInFromRight 0.4s ease-out;
            transition: transform 0.2s ease;
        }

        .file-item:hover {
            transform: translateX(10px);
        }

        @keyframes slideInFromRight {
            0% {
                transform: translateX(30px);
                opacity: 0;
            }
            100% {
                transform: translateX(0);
                opacity: 1;
            }
        }

        .file-item h4 {
            color: #495057;
            margin-bottom: 8px;
            font-size: 1.1em;
        }

        .file-details {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 10px;
            font-size: 0.9em;
            color: #6c757d;
        }

        .loading {
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 3px solid #f3f3f3;
            border-top: 3px solid #667eea;
            border-radius: 50%;
            animation: spin 1s linear infinite;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        .error {
            background: #f8d7da;
            color: #721c24;
            padding: 15px;
            border-radius: 8px;
            border: 1px solid #f5c6cb;
            margin-top: 15px;
        }

        .success {
            background: #d4edda;
            color: #155724;
            padding: 15px;
            border-radius: 8px;
            border: 1px solid #c3e6cb;
            margin-top: 15px;
        }

        .tag {
            display: inline-block;
            background: #e9ecef;
            color: #495057;
            padding: 4px 8px;
            border-radius: 4px;
            font-size: 0.8em;
            margin: 2px;
        }

        .json-output {
            background: #f8f9fa;
            border: 1px solid #e9ecef;
            border-radius: 5px;
            padding: 15px;
            font-family: 'Courier New', monospace;
            font-size: 0.9em;
            white-space: pre-wrap;
            max-height: 300px;
            overflow-y: auto;
            margin-top: 15px;
        }

        .quick-actions {
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
            margin-bottom: 20px;
        }

        .quick-actions .btn {
            animation: fadeInScale 0.5s ease-out;
            animation-fill-mode: both;
        }

        .quick-actions .btn:nth-child(1) { animation-delay: 0.1s; }
        .quick-actions .btn:nth-child(2) { animation-delay: 0.2s; }
        .quick-actions .btn:nth-child(3) { animation-delay: 0.3s; }
        .quick-actions .btn:nth-child(4) { animation-delay: 0.4s; }
        .quick-actions .btn:nth-child(5) { animation-delay: 0.5s; }

        @keyframes fadeInScale {
            0% {
                opacity: 0;
                transform: scale(0.8) rotateY(-90deg);
            }
            100% {
                opacity: 1;
                transform: scale(1) rotateY(0deg);
            }
        }

        @media (max-width: 768px) {
            body {
                padding-top: 2vh;
            }
            
            .content {
                padding: 20px;
            }
            
            .demo-section {
                padding: 20px;
            }
            
            .file-details {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>🚀 跨平台文件选择器服务</h1>
            <p>Web API 演示页面 - 支持 Windows 和 macOS</p>
        </div>

        <input type="file">
        <div class="content">
            <!-- 服务状态 -->
            <div class="status-bar">
                <span class="status-indicator status-offline" id="statusIndicator"></span>
                <span id="statusText">正在检查服务状态...</span>
            </div>

            <!-- 快捷操作 -->
            <div class="demo-section">
                <h2>🎯 快捷操作</h2>
                <div class="quick-actions">
                    <button class="btn btn-primary" onclick="quickSelectImages()">📷 选择图片</button>
                    <button class="btn btn-primary" onclick="quickSelectDocuments()">📄 选择文档</button>
                    <button class="btn btn-primary" onclick="quickSelectMultiple()">📁 多选文件</button>
                    <button class="btn btn-secondary" onclick="checkHealth()">❤️ 检查服务</button>
                    <button class="btn btn-success" onclick="getServiceInfo()">ℹ️ 服务信息</button>
                </div>
            </div>

            <!-- 自定义选择 -->
            <div class="demo-section">
                <h2>🔧 自定义文件选择</h2>
                
                <div class="form-group">
                    <div class="checkbox-group">
                        <input type="checkbox" id="multipleFiles">
                        <label for="multipleFiles">启用多选模式</label>
                    </div>
                </div>

                <div class="form-group">
                    <label for="fileTypes">文件类型过滤 (用逗号分隔，例如: .jpg,.png,.pdf)</label>
                    <input type="text" id="fileTypes" class="form-control" placeholder="留空表示所有文件类型">
                </div>

                <div class="form-group">
                    <label for="initialDir">初始目录 (可选)</label>
                    <input type="text" id="initialDir" class="form-control" placeholder="例如: /Users/用户名/Documents">
                </div>

                <button class="btn btn-primary" onclick="customSelectFiles()">
                    <span id="selectButtonText">🎯 选择文件</span>
                </button>
            </div>

            <!-- 结果显示 -->
            <div class="results" id="results" style="display: none;">
                <h3>📋 选择结果</h3>
                <div id="resultContent"></div>
                <div id="jsonOutput"></div>
            </div>
        </div>
    </div>

    <script>
        const API_BASE = 'http://localhost:7429';
        
        // 页面加载时检查服务状态
        window.onload = function() {
            checkHealth();
        };

        // 检查服务健康状态
        async function checkHealth() {
            try {
                const response = await fetch(`${API_BASE}/api/health`);
                const data = await response.json();
                
                if (response.ok) {
                    updateStatus(true, `服务运行正常 - ${data.platform} ${data.python_version}`);
                } else {
                    updateStatus(false, '服务响应异常');
                }
            } catch (error) {
                updateStatus(false, '无法连接到服务 (请确保服务已启动)');
            }
        }

        // 获取服务信息
        async function getServiceInfo() {
            try {
                showLoading('正在获取服务信息...');
                const response = await fetch(`${API_BASE}/api/info`);
                const data = await response.json();
                
                if (response.ok) {
                    showResult('服务信息', null, data);
                } else {
                    showError('获取服务信息失败');
                }
            } catch (error) {
                showError('获取服务信息失败: ' + error.message);
            }
        }

        // 快捷选择图片
        function quickSelectImages() {
            selectFiles(false, ['.jpg', '.jpeg', '.png', '.gif', '.bmp']);
        }

        // 快捷选择文档
        function quickSelectDocuments() {
            selectFiles(false, ['.pdf', '.doc', '.docx', '.txt']);
        }

        // 快捷多选
        function quickSelectMultiple() {
            selectFiles(true, []);
        }

        // 自定义选择文件
        function customSelectFiles() {
            const multiple = document.getElementById('multipleFiles').checked;
            const fileTypesInput = document.getElementById('fileTypes').value.trim();
            const initialDir = document.getElementById('initialDir').value.trim();
            
            let fileTypes = [];
            if (fileTypesInput) {
                fileTypes = fileTypesInput.split(',').map(type => type.trim());
            }
            
            selectFiles(multiple, fileTypes, initialDir || null);
        }

        // 通用文件选择函数
        async function selectFiles(multiple, fileTypes, initialDir = null) {
            try {
                showLoading(multiple ? '正在打开多选文件对话框...' : '正在打开文件选择对话框...');
                
                const requestData = {
                    multiple: multiple,
                    file_types: fileTypes
                };
                
                if (initialDir) {
                    requestData.initial_dir = initialDir;
                }

                const response = await fetch(`${API_BASE}/api/file-selector`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify(requestData)
                });

                const data = await response.json();
                
                if (response.ok && data.success) {
                    showResult('文件选择结果', data.files, data);
                } else {
                    showError(data.error || '文件选择失败');
                }
            } catch (error) {
                showError('请求失败: ' + error.message);
            }
        }

        // 更新服务状态
        function updateStatus(online, message) {
            const indicator = document.getElementById('statusIndicator');
            const text = document.getElementById('statusText');
            
            indicator.className = `status-indicator ${online ? 'status-online' : 'status-offline'}`;
            text.textContent = message;
        }

        // 显示加载状态
        function showLoading(message) {
            const results = document.getElementById('results');
            const content = document.getElementById('resultContent');
            
            content.innerHTML = `
                <div style="text-align: center; padding: 20px;">
                    <div class="loading"></div>
                    <p style="margin-top: 10px;">${message}</p>
                </div>
            `;
            
            results.style.display = 'block';
            document.getElementById('jsonOutput').innerHTML = '';
        }

        // 显示结果
        function showResult(title, files, fullData) {
            const results = document.getElementById('results');
            const content = document.getElementById('resultContent');
            const jsonOutput = document.getElementById('jsonOutput');
            
            let html = `<div class="success"><strong>${title}</strong></div>`;
            
            if (files && files.length > 0) {
                html += `<p style="margin: 15px 0;"><strong>共选择了 ${files.length} 个文件:</strong></p>`;
                
                files.forEach((file, index) => {
                    html += `
                        <div class="file-item">
                            <h4>📄 ${file.name}</h4>
                            <div class="file-details">
                                <div><strong>路径:</strong> ${file.path}</div>
                                <div><strong>大小:</strong> ${file.size_formatted}</div>
                                <div><strong>类型:</strong> ${file.file_type}</div>
                                <div><strong>扩展名:</strong> ${file.extension}</div>
                                <div><strong>修改时间:</strong> ${file.modified_time}</div>
                                <div><strong>可读:</strong> ${file.is_readable ? '✅' : '❌'}</div>
                                <div><strong>可写:</strong> ${file.is_writable ? '✅' : '❌'}</div>
                            </div>
                        </div>
                    `;
                });
            } else if (fullData && fullData.message) {
                html += `<p style="margin: 15px 0;">${fullData.message}</p>`;
            }
            
            content.innerHTML = html;
            jsonOutput.innerHTML = `<strong>完整JSON响应:</strong>\n${JSON.stringify(fullData, null, 2)}`;
            results.style.display = 'block';
            
            // 滚动到结果区域
            results.scrollIntoView({ behavior: 'smooth' });
        }

        // 显示错误
        function showError(message) {
            const results = document.getElementById('results');
            const content = document.getElementById('resultContent');
            
            content.innerHTML = `<div class="error"><strong>错误:</strong> ${message}</div>`;
            document.getElementById('jsonOutput').innerHTML = '';
            results.style.display = 'block';
            
            // 滚动到结果区域
            results.scrollIntoView({ behavior: 'smooth' });
        }

        // 重置选择按钮状态
        function resetButtonState() {
            document.getElementById('selectButtonText').innerHTML = '🎯 选择文件';
        }

        // 定期检查服务状态
        setInterval(checkHealth, 30000); // 每30秒检查一次
    </script>
</body>
</html>